<?php

function getChildren($cates, $parentID) {
    $items = array();
    foreach ($cates as $cate) {
        if ($cate['parentID'] == $parentID) {
            $items[] = $cate;
        }
    }
    return $items;
}

function cateTr($cates, $cate) {
    $str = "<td  class=''><input class='checkbox' type='checkbox' name='id' value='" . $cate['id'] . "' /></td>";
    $str .= "<td class='center '>|" . str_repeat('&ndash;', substr_count($cate['path'], ',')) . $cate['title'] . "&nbsp;</td>";
    $str .= "<td class='center'>" . $cate['description'] . "&nbsp;</td>";
    $str .= "<td class='center'>";
    $str .= "<a class='btn btn-info' title='编辑' href='" . url('category/edit?id=' . $cate['id']) . "' > <i class='icon-edit icon-white'></i></a>&nbsp;";
    $str .= "<a class='btn btn-danger del' id='" . $cate['id'] . "' title='删除' href='" . url('category/del') . "' > <i class='icon-trash icon-white'></i> </a></td>";
    echo $str . "</tr>";
    $children = getChildren($cates, $cate['id']);
    if (!empty($children)) {
        foreach ($children as $child) {
            cateTr($cates, $child);
        }
    }
}
?>
<div>
    <hr>
    <ul class="breadcrumb">
        <li>
            <a href="#">分类</a> <span class="divider">|</span>
        </li>
        <li>
            <a href="#">列表</a>
        </li>
    </ul>
    <hr>
</div>


<div class="row-fluid sortable">		
    <div class="box span12">
        <div class="box-header" data-original-title>
            <h2><i class="icon-align-justify"></i><span class="break"></span>  <a class="" title="添加" href="<?php echo url('category/add?type=' . $type); ?>">添加</a> </h2>
            <div class="box-icon">
                <a href="#" class="btn-setting"><i class="icon-wrench"></i></a>
                <a href="#" class="btn-minimize"><i class="icon-chevron-up"></i></a>
                <a href="#" class="btn-close"><i class="icon-remove"></i></a>
            </div>
        </div>
        <div class="box-content">
             <div class="dataTables_length">
                <select class="app" name="checkbox">
                    <option value="1">删除</option>
                </select>
                <a class="exec" href="javascript:void(0);">应用</a>
            </div>
            <table class="table table-striped table-bordered bootstrap-datatable datatable">
                <thead>
                    <tr>
                        <th><input id="checkbox" type="checkbox" class="checkbox" /> </th>
                        <th>名称</th>
                        <th>描述</th>
                        <th>动作</th>
                         
                    </tr>
                </thead>   
                <tbody>
                          <?php foreach ($topCate as $cate): ?>
    <?php cateTr($cates, $cate); ?>
<?php endforeach; ?>
                </tbody>
            </table>   
<!--            <div class="">
                <select class="app" name="checkbox">
                    <option value="1">删除</option>
                </select>
                <a class="exec" href="javascript:void(0);">应用</a>
            </div>-->
        </div>
    </div><!--/span-->
</div><!--/row-->

<script type="text/javascript">
    $(document).ready(function(){
        $(".exec").click(function(){
            var app = $(this).prev().val();
            if(app==1)
            {
                var ids = '';
                //获取ID
                $("input[name='id']:checked").each(function(){
                    ids += $(this).val() + ',';
                });
                if(ids.length > 0 ){
                    $.post("<?php echo url('category/del'); ?>",{'id':ids},function(data){
                        $("input[name='id']:checked").parents('tr').remove();
                    });			
                }else{
                    $(this).wBox({html:'错误'}).showBox();
                }
            }
            else{
            }
        });
    });
</script>


